from django.shortcuts import render
from smart_monitor.models import User


def sign_up(request):
    data_set = {"error_info": None}
    user_name = request.POST.get("inputName")
    user_email = request.POST.get("inputEmail")
    user_pwd = request.POST.get("inputPassword")
    user_repeat_pwd = request.POST.get("inputRepeatPassword")
    if user_pwd != user_repeat_pwd:
        data_set["error_info"] = "两次密码不一致！"
        return render(request, "sign_up.html", data_set)
    User.objects.create(user_name=user_name, user_email=user_email, user_pwd=user_pwd)
    return render(request, "sign_up_ok.html", data_set)


class SignUp(object):
    def __init__(self, request):
        self.request = request

    def run(self):
        data_set = {}
        if self.request.method == "POST":
            return sign_up(self.request)

        return render(self.request, "sign_up.html", data_set)
